package com.quectel.util.common;

/**
 * 在这里编写说明
 * 通过多个经纬度数组，获取图形中心点坐标
 *
 * @author: bob
 * @email: bob.yu@quectel.com
 * @date: 2023-03-21 9:10:00
 */
public class PolygonCenterPointUtil {

    public static void main(String[] args) {
        double[][] polygon = {{121.247482, 31.099695},
                {121.24746, 31.099093},
                {121.247675, 31.099107},
                {121.247691, 31.099286},
                {121.24812, 31.099259},
                {121.248141, 31.099479},
                {121.247476, 31.099686}
        };
        double[] center = getCenter(polygon);
        System.out.println(center[0] + ", " + center[1]);
    }

    public static double[] getCenter(double[][] coordinates) {
        double x = 0, y = 0, z = 0;
        int len = coordinates.length;
        for (double[] coord : coordinates) {
            double lat = Math.toRadians(coord[1]);
            double lon = Math.toRadians(coord[0]);
            x += Math.cos(lat) * Math.cos(lon);
            y += Math.cos(lat) * Math.sin(lon);
            z += Math.sin(lat);
        }
        x /= len;
        y /= len;
        z /= len;
        double lon = Math.atan2(y, x);
        double hyp = Math.sqrt(x * x + y * y);
        double lat = Math.atan2(z, hyp);
        return new double[]{Math.toDegrees(lon), Math.toDegrees(lat)};
    }
}



